# coding:utf-8
import requests
from hashlib import md5


class Verify:

    def __init__(self, username, password, soft_id='96001'):
        self.username = username
        # 避免明文外泄，可以使用md5后的字符串
        # password = password.encode('utf8')
        # self.password = md5(password).hexdigest()
        self.password = password
        self.soft_id = soft_id
        self.base_params = {
            'user': self.username,
            'pass2': self.password,
            'softid': self.soft_id,
        }
        self.headers = {
            'Connection': 'Keep-Alive',
            'User-Agent': 'Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0)',
        }

    def PostPic(self, codetype, file_base64=None, im='./lib/verify/verify.png'):
        """
        im: 图片字节
        codetype: 验证码类型 参考 http://www.chaojiying.com/price.html
        """

        params = {
            'codetype': codetype,
        }
        params.update(self.base_params)
        # 如果不是base64图片，就去掉保存好的验证码图片
        if file_base64 is None or file_base64 == "":
            # 读取图片为二进制
            im = open(im, 'rb').read()
            files = {'userfile': ('verify.jpg', im)}
            try:
                r = requests.post('http://upload.chaojiying.net/Upload/Processing.php',
                                  data=params, files=files,
                                  headers=self.headers)
                rjson = r.json()
                if rjson['pic_str'] is None or str(rjson['pic_str']) == '':
                    return None
                else:
                    return str(rjson['pic_str'])
            except Exception as e:
                return None
        else:
            # base64图片，直接传base64
            params.update({"file_base64": file_base64})
            try:
                r = requests.post('http://upload.chaojiying.net/Upload/Processing.php',
                                  data=params,
                                  headers=self.headers)
                rjson = r.json()
                if rjson['pic_str'] is None or str(rjson['pic_str']) == '':
                    return None
                else:
                    return str(rjson['pic_str'])
            except Exception as e:
                return None

    def ReportError(self, im_id):
        """
        im_id:报错题目的图片ID
        """
        params = {
            'id': im_id,
        }
        params.update(self.base_params)
        r = requests.post('http://upload.chaojiying.net/Upload/ReportError.php', data=params, headers=self.headers)
        return r.json()


if __name__ == '__main__':
    verify = Verify('wuqingfqng', '6e8ebd2e301f3d5331e1e230ff3f3ca5', '96001')
    # base64图片解析
    # print(verify.PostPic(1902,file_base64="R0lGODdhlgA8AIcAAP7+/gICAujo6NfX1/Pz8xcXFycnJ1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oGCAO3bA4AfH4CAAgHsKwCQX39+AgAAACTgwAABAAAGGIAQYOHCBxMAQIRI\nAADFihYpDgCgcSPHjh43NhjAIECBBgIGAEipMqUAAwkQAIgpM2aEAhEEAP/IqXMnz5wPAAANKlRA\ngKIBCjgwAGApUwADDASImmAAgKpWIwgAoHWrVgEJAoAFe0ABAwBmz6JNO4AAgLZu38KN+1bAAgUB\n7i4QIAAA3758JQQIcAABgMKGDQeYgAAA48aOGwsAIHky5ckRAmDOrAAA584AJAyQ0CBAAAQATp8e\n0AAA69atDwRYEGB2AAUNBADIrXs3AAIEBAAILnw48eLGIRQIEKBAggUHCACILn26hAcMBBAAoF3A\ngAEAvn8PEMABAQECBgwAIAAA+/bu3wNwUKBAgPoBIADIrx8AAgD+AQIYIAABAwAHESZMCCFAwwAF\nHBAwAIBiRYoCEADQuJGnY0ePHzk+KFAgQAAFAwgQALCSZcuWBADElDmTZk2bN2MaUADBQACfBAAE\nFToAQFGjR5EmRTqAgYIDBR4IQAABAgCrV7Fm1bqV61UHAcCGVQCALFkCAwYAULuWbVu3b+HGZTuA\nQQEHAxQoALCXb1+/fwEHFjyYcGG+BBA0UGwgAADHjyFHljyZcmXLlzFn1ryZc2fPn0GHFj2adGnT\np1GnVr2adevKAQEAOw==\n"))

    # 普通图片解析
    print(verify.PostPic(1902, im='../lib/images/verify.png'))
